package com.example.isoftservice.service.impl;


import com.example.isoftservice.mapper.ProductMapper;
import com.example.isoftservice.pojos.Product;
import com.example.isoftservice.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassNmae:ProductServiceImpl
 * Descriptiopn:
 *
 * @Create 2023/10/27 14:48
 * @Author yidao
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;


    //查询经销商信息
    @Override
    @Cacheable(cacheNames ="address",key="#addressId")
    public Map<String, Object> searchProductInfo(Product product) {
        HashMap<String, Object> map = new HashMap<>();

        PageHelper.startPage(product.getPage(),product.getRows());
//    注意这里的执行顺序，必须先执行PageHelper.startPage，否则分页效果不实现
        List<Map<String, Object>> list =productMapper.select(product);


        PageInfo<Map<String,Object>> pageInfo= new PageInfo<>(list);
        //获取分页后的信息
        map.put("code",0);
        map.put("meaaage","成功");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());

        return map;

    }

    @Override
    public Map<String, Object> searchCategoryInfo() {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list =productMapper.selectCategory();
        map.put("code",0);
        map.put("message","成功");
        map.put("data",list);
        return map;
    }

    @Override
    public Map<String, Object> removeProductById(Product product) {
        Map<String,Object> map =new HashMap<>();

        Integer count=productMapper.delete(product);
        if(count >=1){
            map.put("flag",0);
            map.put("code",true);
            map.put("message","删除成功");
        }else{
            map.put("flag",1);
            map.put("code",false);
            map.put("message","删除失败");
        }
        return map;
    }

    @Override
    @CacheEvict(cacheNames = "produt" ,allEntries = true)
    public Map<String, Object> addProductInfo(Product product) {
        Map<String,Object> map =new HashMap<>();

        Integer count=productMapper.insert(product);
        if(count ==1){
            map.put("flag",0);
            map.put("code",true);
            map.put("message","添加成功");
        }else{
            map.put("flag",1);
            map.put("code",false);
            map.put("message","添加失败");
        }
        return map;
    }

    @Override
    @CacheEvict(cacheNames = "produt" ,allEntries = true)
    public Map<String, Object> modifyProductById(Product product) {
        Map<String,Object> map =new HashMap<>();

        Integer count=productMapper.update(product);
        if(count >=1){
            map.put("flag",0);
            map.put("code",true);
            map.put("message","更新成功");
        }else{
            map.put("flag",1);
            map.put("code",false);
            map.put("message","更新失败");
        }
        return map;
    }

}
